package com.ding.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ding.eduservice.entity.EduSubject;
import com.ding.eduservice.entity.excel.Excel;
import com.ding.eduservice.entity.excel.SubjectListener;
import com.ding.eduservice.entity.subject.Onesubject;
import com.ding.eduservice.entity.subject.Twosubject;
import com.ding.eduservice.mapper.EduSubjectMapper;
import com.ding.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
         EasyExcel.read(in, Excel.class,new SubjectListener(eduSubjectService)).sheet("课程分类").doRead();
        }catch (Exception e){
           e.printStackTrace();
        }

    }

    @Override
    public List<Onesubject> getSubject() {
        //查询出所有的一级分类
        QueryWrapper<EduSubject> wrapperone = new QueryWrapper<>();
        wrapperone.eq("parent_id","0");

        //baseMapper 表示获取mapper方法
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapperone);
        //查询出所有的二级分类
        QueryWrapper<EduSubject> wrappertwo = new QueryWrapper<>();
        wrappertwo.ne("parent_id","0");

        //baseMapper 表示获取mapper方法
        List<EduSubject> twoSubjects = baseMapper.selectList(wrappertwo);


        //创建list集合，用于存储最终封装的数据
        List<Onesubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        //查询出来的所有的一级分类list集合做遍历，得到每一个一级分类
        for (int i = 0; i < oneSubjects.size(); i++) {
            EduSubject eduSubject = oneSubjects.get(i);

            //把eduSubject里面值获取出来，放到OneSubject对象里面

            //多个onesubject放到finalSubjectList里面
            Onesubject onesubject = new Onesubject();
//            onesubject.setId(eduSubject.getId());
//            onesubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,onesubject);

            finalSubjectList.add(onesubject);

            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<Twosubject> twoobjects = new ArrayList<>();
            for (int j = 0; j < twoSubjects.size(); j++) {
                EduSubject eduSubject1 = twoSubjects.get(j);
                //判断二级分类parentid和一级分类id是否一样
                if(eduSubject1.getParentId().equals(eduSubject.getId())){
                    //把eduSubject1的值复制到Twoobject里面，放到twoobjects里面去
                    Twosubject twosubject = new Twosubject();
                    BeanUtils.copyProperties(eduSubject1,twosubject);
                    twoobjects.add(twosubject);
                }
            }

            //把一级下面所有的二级分类放到一级分类里去
            onesubject.setChildren(twoobjects);

        }

        //封装二级分类
       return finalSubjectList;

    }
}
